#pragma once

#include <stddef.h>

/**
 * @brief 双向链表节点结构
 */
struct DList {
    DList *prev = NULL;  ///< 前驱节点指针
    DList *next = NULL;  ///< 后继节点指针
};

/**
 * @brief 初始化双向链表节点
 * @param node 要初始化的节点
 */
inline void dlist_init(DList *node) {
    node->prev = node->next = node;
}

/**
 * @brief 检查链表是否为空
 * @param node 链表头节点
 * @return 如果为空返回true，否则返回false
 */
inline bool dlist_empty(DList *node) {
    return node->next == node;
}

/**
 * @brief 从链表中分离节点
 * @param node 要分离的节点
 */
inline void dlist_detach(DList *node) {
    DList *prev = node->prev;
    DList *next = node->next;
    prev->next = next;
    next->prev = prev;
}

/**
 * @brief 在目标节点前插入新节点
 * @param target 目标节点
 * @param rookie 要插入的新节点
 */
inline void dlist_insert_before(DList *target, DList *rookie) {
    DList *prev = target->prev;
    prev->next = rookie;
    rookie->prev = prev;
    rookie->next = target;
    target->prev = rookie;
} 